package com.yj.ccs.abs.boot.pagination;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/8
 * Author     : Unbelievable9
 **/
@Data
public class PageDto<T> {

    @ApiModelProperty(value = "分页信息-页数(从1开始）", example = "1")
    private Integer pageNumber = 1;

    @ApiModelProperty(value = "分页信息-单页元素数量", example = "10")
    private Integer pageSize = 10;

    @ApiModelProperty(hidden = true)
    private Integer totalItem;

    @ApiModelProperty(hidden = true)
    private Integer totalPage;

    @ApiModelProperty(hidden = true)
    private List<T> pageResult;

    public void updateTotalItem(Integer totalItem) {
        this.totalItem = totalItem;

        Integer theTotalPage = totalItem % pageSize == 0 ? totalItem / pageSize : totalItem / pageSize + 1;

        if (theTotalPage > 0) {
            this.pageNumber = this.pageNumber > theTotalPage ? theTotalPage : this.pageNumber;
        }

        this.setTotalPage(theTotalPage);
    }
}
